package com.devshephard.test;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadCross {

    public static void main(String[] args) throws InterruptedException {

        AtomicInteger atomicInteger = new AtomicInteger(0);

        Object obj = new Object();



        Thread thread1 = new Thread(() -> {

            while (true){

                if(atomicInteger.get() == 0){
                    System.out.println("A");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    atomicInteger.compareAndSet(0,1);

                }

            }

        }, "Print A");

        TimeUnit.SECONDS.sleep(1);



        Thread thread2 = new Thread(() -> {

            while (true){

                if(atomicInteger.get() == 1){
                    System.out.println("B");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    atomicInteger.compareAndSet(1,2);
                }

            }

        }, "Print B");

        TimeUnit.SECONDS.sleep(1);

        Thread thread3 = new Thread(() -> {

            while (true){

                if(atomicInteger.get() == 2){
                    System.out.println("C");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    atomicInteger.compareAndSet(2,0);
                }

            }

        }, "Print B");

         thread1.start();
         thread2.start();
         thread3.start();

    }

}
